package com.huaxin.hxcloud.work;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;

import com.huamaitel.api.HMDefines;
import com.huamaitel.api.HMJniInterface;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseApp;
import com.huaxin.common.Entity.HxVideoInfoDTO;
import com.huaxin.common.tools.HxTransmitRequestHelper;
import com.huaxin.common.tools.RequestHelper;
import com.huaxin.common.utils.ToastUtil;
import com.huaxin.hxcloud.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author songxf
 *         created at 2017/7/10 15:02
 * @desc 对接华迈视频
 */

public class HxHuaMaiVideoActivity extends BaseActivity {


    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.toolbar)
    Toolbar      mToolbar;


    private List<Map<String, Object>> mListData;
    private HxHuaMaiAdapter           mAdapter;
    private String                    mStationCode;
    private List<HxVideoInfoDTO.VideoInfoBean> mVideoInfoBeen = new ArrayList<>();
    private int mRootId;

    private static final String TAG = "HxHuaMaiVideoActivity";

    @Override
    protected int getContentViewId() {
        return R.layout.activity_huamai_video;
    }

    @Override
    public void initView() {
        Intent intent = getIntent();
        mStationCode = intent.getStringExtra("stationCode");

        Log.d(TAG, "initView: " + mStationCode);

        //必须要初始化
        BaseApp.getJni().init();
        mListData = new ArrayList<Map<String, Object>>();
        //获取节点
        int treeId = BaseApp.treeId;
        mRootId = BaseApp.getJni().getRoot(treeId);
        BaseApp.rootList.clear();
        BaseApp.rootList.add(mRootId);
        Log.d(TAG, "rootId-->: " + mRootId);

        mRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerview.setHasFixedSize(true);

        mAdapter = new HxHuaMaiAdapter(this, mListData);
        mRecyclerview.setAdapter(mAdapter);

        ApiRequsetData(mStationCode);

        setSupportActionBar(mToolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    public void initData() {
    }

    @Override
    public int getLoadFrameId() {
        return R.id.content;
    }


    /**
     * @desc 请求网络数据
     * @author songxf
     * created at 2017/7/12 8:42
     */
    private void ApiRequsetData(String stationCode) {
        updateLoadStatus(LOADING);
        Call<HxVideoInfoDTO> videoInfo = HxTransmitRequestHelper.getRequestApi().getVideoInfo(stationCode);
        autoClose(videoInfo);
        Log.d(TAG, "onResponse1: " + "11111");

        videoInfo.enqueue(new Callback<HxVideoInfoDTO>() {
            @Override
            public void onResponse(Call<HxVideoInfoDTO> call, Response<HxVideoInfoDTO> response) {
                Log.d(TAG, "onResponse1: " + "222222");
                if (response.isSuccessful()) {
                    Log.d(TAG, "onResponse1: " + "33333");
                    HxVideoInfoDTO body = response.body();
                    mVideoInfoBeen = body.getVideoInfo();
                    Log.d(TAG, "onResponse1: " + (mVideoInfoBeen == null));
                    Log.d(TAG, "onResponse2: " + (mVideoInfoBeen.size() == 0));
                    if (mVideoInfoBeen == null || mVideoInfoBeen.size() == 0) {
                        updateLoadStatus(LOAD_NO_DATA);
                        Log.d(TAG, "onResponse1: " + "444444");
                        return;
                    }
                    updateLoadStatus(LOAD_SUCCESS);
                    getChildrenByNodeId(mRootId, mVideoInfoBeen);
                    mAdapter.notifyDataSetChanged();
                    return;
                }
                Log.d(TAG, "onResponse1: " + "55555");
                ToastUtil.showToast("数据异常");
                updateLoadStatus(LOAD_ERROR);
            }

            @Override
            public void onFailure(Call<HxVideoInfoDTO> call, Throwable t) {
                ToastUtil.showToast("网络故障");
                updateLoadStatus(LOAD_FAILURE);
            }
        });
    }

    //获取节点信息
    private void getChildrenByNodeId(int nodeId, List<HxVideoInfoDTO.VideoInfoBean> videoInfoBeen) {
        if (nodeId != 0) {
            HMJniInterface sdk = BaseApp.getJni();
            mListData.clear();
            //视频节点
            int count = sdk.getChildrenCount(nodeId);
            for (int i = 0; i < videoInfoBeen.size(); i++) {
                String videoSN = videoInfoBeen.get(i).getVideoSN();
                for (int j = 0; j < count; j++) {
                    Map<String, Object> flagMap = new HashMap<String, Object>();
                    int childrenNode = sdk.getChildAt(nodeId, j);
                    if (videoSN.equals(sdk.getDeviceSn(childrenNode))) {
                        Log.d(TAG, "getChildrenCount: " + count);
                        int nodeType = sdk.getNodeType(childrenNode);
                        flagMap.put("type", nodeType);
                        if (nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_GROUP) {
                            // obj.put("img", R.drawable.folder);
                        } else if (nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_DEVICE) {
                            //  obj.put("img", R.drawable.device);
                        } else if (nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_DVS) {
                            //  obj.put("img", R.drawable.dvs);
                        } else if (nodeType == HMDefines.NodeTypeInfo.NODE_TYPE_CHANNEL) {
                            //  obj.put("img", R.drawable.device);
                        }
                        Log.d(TAG, " childNode: " + childrenNode);
                        Log.d(TAG, "childNode Url: " + sdk.getNodeUrl(childrenNode));
                        Log.d(TAG, "childNode sn: " + sdk.getDeviceSn(childrenNode));
                        flagMap.put("id", childrenNode);
                        flagMap.put("name", sdk.getNodeName(childrenNode));
                        mListData.add(flagMap);
                    }
                }
            }

        }
    }
}
